﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using ConstructionBidOnline.Biz;
using ConstructionBidOnline.Common;

public partial class ManageBuyers : Page
{
    #region Types

    public enum LayoutType
    {
        Add,
        Edit,
        Search,
        View
    }

    #endregion

    #region Constants

    private const string PROP_DATASRC_USERS = "__USERS::USERS";
    private const string PROP_LAYOUT = "__USERS::UIL";
    private const string PROP_SELECTED_USERS_ID = "__USERS::SUSERSID";

    #endregion

    #region Public Properties

    public RegisteredUserCollection DataSourceRegisteredUsers
    {
        get { return (SessionMgr.Session[PROP_DATASRC_USERS] is RegisteredUserCollection ? (RegisteredUserCollection)SessionMgr.Session[PROP_DATASRC_USERS] : (RegisteredUserCollection)(SessionMgr.Session[PROP_DATASRC_USERS] = new RegisteredUserCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_USERS] = value; }
    }

    public int SelectedUserID
    {
        get { return (SessionMgr.Session[PROP_SELECTED_USERS_ID] is int ? (int)SessionMgr.Session[PROP_SELECTED_USERS_ID] : -1); }
        set { SessionMgr.Session[PROP_SELECTED_USERS_ID] = value; }
    }


    public LayoutType UILayout
    {
        get { return (SessionMgr.Session[PROP_LAYOUT] is LayoutType ? (LayoutType)SessionMgr.Session[PROP_LAYOUT] : LayoutType.Search); }
        set { SessionMgr.Session[PROP_LAYOUT] = value; }
    }


    #endregion

    #region Event Handlers

    #region Page Load

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        if (!Page.IsPostBack)
        {
            DataSourceRegisteredUsers = RegisteredUser.GetAllRegisteredUsersByType("Buyer");
            BuyersList1.DataBind(DataSourceRegisteredUsers);
        }
    }

    #endregion

    #region Control: BuyersList

    protected void ucBuyersList_EditRequested(object sender, Controls_BuyersList.EditRequestedEventArgs evt)
    {
        SelectedUserID = evt.RU.RegisteredUserID;
        BuyersViewEdit1.DataBind(evt.RU);
        SetLayout(LayoutType.Edit);
    }


    protected void ucBuyersList_AddNewRequested(object sender, EventArgs evt)
    {
        //BuyersViewEdit1.CancelEdit();
        //RegisteredUser newRegisteredUser= RegisteredUser.CreateNew(DataSourceRegisteredUsers);
        //SelectedUserID = newRegisteredUser.RegisteredUserID;
        //BuyersViewEdit1.DataBind(DataSourceRegisteredUsers.FindByRegisteredUserID(SelectedUserID));
        //SetLayout(LayoutType.Edit);
    }


    protected void ucBuyersList_DataSourceRequested(object sender, EventArgs evt)
    {
        DataSourceRegisteredUsers = RegisteredUser.GetAllRegisteredUsersByType("Buyer");
        BuyersList1.DataBind(DataSourceRegisteredUsers);
    }

    #endregion


    #region Control: BuyersViewEdit

    protected void ucBuyersList_DataSourceBuyerRequested(object sender, EventArgs evt)
    {
        BuyersViewEdit1.DataSourceBuyer = DataSourceRegisteredUsers.FindByRegisteredUserID(SelectedUserID);
    }

    #endregion

    #region Save / Cancel Buttons

    protected void btnCancel_Click(object sender, ImageClickEventArgs e)
    {
        BuyersViewEdit1.CancelEdit();
        // If we were adding a new Consultant...then remove the incomplete/not added new Consultant object from the collection.
        if (DataSourceRegisteredUsers.FindByRegisteredUserID(SelectedUserID).ObjState == BOBase.ObjectState.Added)
        {
            try
            {
                DataSourceRegisteredUsers.Remove(DataSourceRegisteredUsers.FindByRegisteredUserID(SelectedUserID));
                SelectedUserID = (DataSourceRegisteredUsers[0] != null ? DataSourceRegisteredUsers[0].RegisteredUserID : -1);
            }
            catch
            {
            }
        }
        BuyersList1.DataBind(DataSourceRegisteredUsers);
        SetLayout(LayoutType.View);
    }
    protected void btnsave_Click(object sender, ImageClickEventArgs e)
    {
        if (BuyersViewEdit1.EndEdit())
        {
            RegisteredUser obj = DataSourceRegisteredUsers.FindByRegisteredUserID(SelectedUserID);
            //string emailfeedback;
            //bool emailsend = ConstructionBidOnline.MailUtils.SendEmail(obj.EmailAddress.ToString(), obj.UserName, "Confirm Registration...", "Dear " + obj.FirstName + " " + obj.LastName + "<br><br>" + "Welcome to Construction Bid Online. You have been registered to ConstructionBidOnline as a consultant, Thanks for giving your time. <br><br> Your Login ID for constructionbidonline.com is : " + obj.UserName + "<br><br>and password to this ID is " + obj.Password + "<br><br> You also get a Pop Mail Account with Id : " + obj.CBOPopMailAccount + "<br><br>with password : " + obj.CBOPopMailPassword + "<br><br><br>This is a system generated mail.<br><br>So Please dont reply to this mail.", out emailfeedback);

            RegisteredUser.Update(obj);
            //Set the feedback text here.....
            BuyersList1.DataBind(DataSourceRegisteredUsers);
            SetLayout(LayoutType.View);
            Response.AddHeader("Refresh", "0");
        }
    }


    #endregion

    #endregion

    #region Private

    #region SetLayout

    /// <summary>
    /// Configures the UI for the specified 'uiLayoutType'.
    /// </summary>
    protected void SetLayout(LayoutType uiLayoutType)
    {
        switch (uiLayoutType)
        {
            case LayoutType.Add:
            case LayoutType.Edit:

                pnlBuyersList.Visible = false;
                pnlBuyersViewEdit.Visible = true;
                pnlButtons.Visible = true;
                break;

            case LayoutType.View:
            case LayoutType.Search:

                pnlBuyersList.Visible = true;
                pnlBuyersViewEdit.Visible = false;
                pnlButtons.Visible = false;
                break;

            default:
                throw new NotImplementedException("The specified LayoutType is not supported in this release.");
        }

        //SetInitialFocus(uiLayoutType);
    }

    #endregion

    #endregion


}
